package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.Schoolyear;
import com.rico.deploy.service.SchoolyearService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 夏智力
 * @FileName SchoolyearController
 * @date 2021/5/6 14:40
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class SchoolyearController {

    @Autowired
    private SchoolyearService schoolyearService;


    @GetMapping("schoolyear/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        List<Schoolyear> schoolyears = schoolyearService.selectAll(page, limit);
        PageInfo<Schoolyear> schoolyearPageInfo = new PageInfo(schoolyears);
        return MessageUtils.success(schoolyearPageInfo.getList(), (int) schoolyearPageInfo.getTotal());
    }

    @GetMapping("schoolyear/lists")
    @ResponseBody
    public RestResponse getlist() {
        return MessageUtils.success(schoolyearService.selectAll());
    }

    @GetMapping("schoolyear/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( schoolyearService.deleteSchoolyearById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }


    @PostMapping("schoolyear/put")
    @ResponseBody
    public RestResponse put(Schoolyear schoolyear) {


        int i = schoolyearService.updateSchoolyear(schoolyear);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    @PostMapping("schoolyear/post")
    @ResponseBody
    public RestResponse post(Schoolyear schoolyear) {

        int i = schoolyearService.insertSchoolyear(schoolyear);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 获取html页面
     * @return
     */
    @GetMapping("schoolyearManage")
    public String gethtml(){
        return "page/schoolyearManage";
    }


}
